package com.ymttest.business.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;

import com.ymatou.productstock.api.parameter.CatalogBean;
import com.ymatou.productstock.api.parameter.CatalogBean.StockBean;
import com.ymt.core.tool.Logger;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.utils.MapUtil;

public class ProductStockCallService {
	
	public static void checkNewMongoCatalogStock(String productId,List<CatalogBean> tgtCatalogBeanList) throws Exception{
		checkNewMongoCatalogStock( productId, tgtCatalogBeanList, true);
	}
	/***
	 * 检查新mongo库 库存
	 * productDetailModel
	 * productListingModel
	 * @param productId
	 * @param tgtCatalogBeanList
	 * @throws Exception
	 */
	
	public static void checkNewMongoCatalogStock(String productId,List<CatalogBean> tgtCatalogBeanList,boolean ischeckRecord) throws Exception{
		//productDetailModel
		JSONArray CatalogList = new JSONArray(new ProductDetailModelWapper().getProductId(productId).get("CatalogList").toString());
		
		if (ischeckRecord){
			Logger.verifyEquals(tgtCatalogBeanList.size(), CatalogList.length(), "productDetailModel 规格数量");
		}
		
		for (CatalogBean catalogBean : tgtCatalogBeanList){
			String catalogId = catalogBean.getCatalogId();
			boolean isFoundCatalog = false;
			
			for(int i=0;i<CatalogList.length();i++){
				if (CatalogList.getJSONObject(i).getString("CatalogId").equals(catalogId)){
					//逐个检查库存
					JSONArray StockList = CatalogList.getJSONObject(i).getJSONArray("StockList");
					List<StockBean> stockBeanList = catalogBean.getStockBean();
					Logger.verifyEquals(stockBeanList.size(), StockList.length(), "productDetailModel 库存记录数量");
					
					
					for(StockBean stockBean: stockBeanList){
						boolean isFoundStock = false;
						for (int j=0;j<StockList.length();j++){
							//-1 为共享库存
							if (stockBean.getProductInActivityId() == -1){
								if(stockBean.getStockType() == StockList.getJSONObject(j).getInt("StockType") &&stockBean.getStockType()==0){
									AssertService.assertResultEqual(MapUtil.hashMap("StockType", 0, "Stock",stockBean.getStock(),"ProductInActivityId",0),
											MapUtil.jsonToMap(StockList.getJSONObject(j)),"ProductDetailModel - StockList 共享库存");
									isFoundStock = true;
									break;
								}
							}else{//活动库存
								if(stockBean.getProductInActivityId() == StockList.getJSONObject(j).getInt("ProductInActivityId") &&stockBean.getStockType()==1){
									AssertService.assertResultEqual(MapUtil.hashMap("StockType", 1, "Stock", stockBean.getStock(),"ProductInActivityId",stockBean.getProductInActivityId()), 
											MapUtil.jsonToMap(StockList.getJSONObject(j)), "ProductDetailModel - StockList 活动库存");
									isFoundStock = true;
									break;
								}
							}
						}
						Logger.verifyEquals(true, isFoundStock, "Stock:["+stockBean.getStock()+"] should be exist in productDetailModel");
					}
					
					isFoundCatalog = true;
					break;
				}    
			}
			Logger.verifyEquals(true, isFoundCatalog, "CatalogId:["+catalogId+"] should be exist in productDetailModel");
			
		}
		
		
		//productListingModel
		Map<String, Object> productListingModelMap = new ProductListingModelWapper().getProductId(productId);
	
		int tgtCatalogStock = 0;
		Map<Integer, Integer> ActivityStockMap = new HashMap<Integer,Integer>();
		for(CatalogBean catalogBean : tgtCatalogBeanList){
			for(StockBean stockBean:catalogBean.getStockBean()){
				//共享库存总计
				if (stockBean.getStockType()==0){
					tgtCatalogStock = tgtCatalogStock + stockBean.getStock();
				}else{
					//活动库存
					if (ActivityStockMap.containsKey(stockBean.getProductInActivityId())){
						Integer oldStock = ActivityStockMap.get(stockBean.getProductInActivityId());
						ActivityStockMap.put(stockBean.getProductInActivityId(),oldStock+stockBean.getStock());
					}else{
						ActivityStockMap.put(stockBean.getProductInActivityId(), stockBean.getStock());
					}
					
				}
			}
		}
		
		//活动库存检查
		if (ActivityStockMap.size()>0){
			if ( !(productListingModelMap.get("ActivityStockList") == null)){
				JSONArray activityStockListBean = new JSONArray(productListingModelMap.get("ActivityStockList").toString());
				Logger.verifyEquals(ActivityStockMap.size(), activityStockListBean.length(), "ProductListingModel - ActivityStockList 个数");
				for(int k=0;k<activityStockListBean.length();k++){
					Integer ProductInActivityId = activityStockListBean.getJSONObject(k).getInt("ProductInActivityId");
					if (ActivityStockMap.containsKey(ProductInActivityId)){
						AssertService.assertResultEqual(MapUtil.hashMap( "ActivityStock", ActivityStockMap.get(ProductInActivityId),"ProductInActivityId",ProductInActivityId), 
								MapUtil.jsonToMap(activityStockListBean.getJSONObject(k)), "ProductListingModel - ActivityStockList 活动库存");
					}
				}
			}else{
				Logger.verifyNotNull(productListingModelMap.get("ActivityStockList"), "productListingModel - ActivityStockList");
			}
		}
		Logger.verifyEquals(tgtCatalogStock, Integer.parseInt(productListingModelMap.get("CatalogStock").toString()), "ProductListingModel - CatalogStock");
		
	}
	
}
